package com.dingshi.security.api;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import com.dingshi.security.config.Config;
import com.dingshi.security.log.SSLog;
import com.dingshi.security.net.HttpResult;
import com.dingshi.security.net.HttpResultJson;
import com.dingshi.security.net.NetService;

public class WeatherApi extends BaseApi {

	protected static final String TAG = "WeatherApi";
	
	public static final String CITYID = "cityid";
	public static final String CITY = "city";
	public static final String CITY_EN = "city_en";
	public static final String DATE_Y = "date_y";
	public static final String WEEK = "week";
	
	public static final String TEMPERATURE = "temp1";//摄氏温度
	public static final String WEATHER = "weather1";//天气描述
	public static final String IMG = "img1";//天气描述图片序号
	public static final String IMG_TITLE = "img_title1";// 图片名称 
	public static final String WIND = "wind1";//风速描述
	public static final String FL = "fl1";//风速级别描述

	public WeatherApi(Context context) {
		super(context);
	}
	
	public void getWeather(final int requestCode) {
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);

				HttpResultJson result = NetService.httpGetReturnJson(context,
						Config.WEATHER_URL, null);
				
				SSLog.e(TAG,"weather " + result.getJson());

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject json = result.getJson();
					Map<String, String> map = parserJson(json);
					apiResult.setEntity(map);

				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}


			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}
		}.execute("");
	}

	
	private Map<String,String> parserJson(JSONObject json) {
		HashMap<String,String> map = null;
		try {
			if(json!=null) {
				map = new HashMap<String, String>();
				JSONObject jsonObject = json.getJSONObject("weatherinfo");
				String temp = jsonObject.getString(TEMPERATURE);
				String weather = jsonObject.getString(WEATHER);
				String img = jsonObject.getString(IMG);
				String img_title = jsonObject.getString(IMG_TITLE);
				String wind = jsonObject.getString(WIND);
				String fl = jsonObject.getString(FL);
				map.put(TEMPERATURE, temp);
				map.put(WEATHER, weather);
				map.put(IMG, img);
				map.put(IMG_TITLE, img_title);
				map.put(WIND, wind);
				map.put(FL, fl);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return map;
		
	}
	
}
